package com.get.backstage.flowable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.get.backstage.common.result.R;
import com.get.backstage.flowable.entity.FlowableEntity;
import com.get.backstage.user.entity.User;
import com.get.backstage.user.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Api(tags = "流程定义")
@RestController
@RequestMapping("/process")
@Slf4j
public class FlowableController {
    @Autowired
    ProcessEngine processEngine;
    @Autowired
    UserService userService;


    /**
     * 流程定义
     */
    @PostMapping("/definition")
    @Transactional(rollbackFor = Exception.class)
    public R usernameLogin(FlowableEntity flowableEntity){

        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .name(flowableEntity.getName())
                .addClasspathResource(flowableEntity.getXmlAddress())
                .deploy();
        return R.success(flowableEntity.getName()+"部署成功");
    }



    /**
     * 创建流程实例
     */
    @RequestMapping("/instance")
    public String processInstance(User user) throws IOException {


        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("approvalType1","张三");
        variables.put("approvalType2","李四");
        variables.put("user",user);
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("employment_application_key", variables);

        return "启动流程实例成功";
    }













}
